from django.contrib import admin

# Register your models here.
from django import forms

from qn_web import models


class PlateAdmin(admin.ModelAdmin):
    list_display = ('name', 'module', 'image', 'show_index', 'z_index')


class ModulesAdmin(admin.ModelAdmin):
    list_display = ('name', 'url')


class SWAdmin(admin.ModelAdmin):
    list_display = ('name', 'phone', 'icon', 'pic', 'url')


class FForm(forms.ModelForm):
    content = forms.CharField(widget=forms.Textarea)

    class Meta:
        model = models.Footer
        fields = '__all__'


class FooterAdmin(admin.ModelAdmin):
    form = FForm


class NewsAdmin(admin.ModelAdmin):
    list_display = ('name', 'pic', 'tag', 'belong_plate', 'up_time')
    list_filter = ('tag', 'belong_plate')
    # list

    def save_model(self, request, obj, form, change):
        if request.POST.get('as_banner', False) and 'True' in str(obj.belong_plate.has_news.values_list('as_banner', flat=True)):
            raise AssertionError('板块已经有了banner。')
        obj.save()


admin.site.site_header = '青年学院官网后台'
admin.site.site_title = '青年学院'

admin.site.register(models.Footer, FooterAdmin)
admin.site.register(models.Modules, ModulesAdmin)
admin.site.register(models.Plates, PlateAdmin)
admin.site.register(models.SettingWeb, SWAdmin)
admin.site.register(models.News, NewsAdmin)
